Ext.onReady(function() {
	
	//显示提示信息
	Ext.QuickTips.init();
	
	//统一设置提示风格，为side
	Ext.form.Field.prototype.msgTarget='side';	
	
	//片路径：一张1×1的透明图片，Ext使用它来和CSS一起创建内嵌的图标。 
	Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//数据源
 	var myTroubleTicketStore = new Ext.data.JsonStore({
			url: '../myTroubleTicket/list.action',
			fields: ['id', 'desc', 'level', 'location', 'happenTime', 'createTime', 'status'],
			root: 'data', 
			totalProperty: 'total' 
		});
	myTroubleTicketStore.load({
		params: {
			start: 0,
			limit: 10
		}
	});	
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//列模型
	var colModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		selModel, //加入checkbox选择模型
		{header: '描述', width: 100, dataIndex: 'desc', sortable: true},
		{header: '级别', width: 100, dataIndex: 'level', sortable: true},
		{header: '班级', width: 100, dataIndex: 'location', sortable: true},
		{header: '发生时间', width: 100, dataIndex: 'happenTime', sortable: true},
		{header: '创建时间', width: 100, dataIndex: 'createTime', sortable: true},
		{header: '状态', width: 100, dataIndex: 'status', sortable: true}
	])
	colModel.defaultWidth  = 100;
	
	var myTroubleTicketGrid = new Ext.grid.GridPanel({
		title: '我的请假单列表',
		height: 450,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		sm: selModel, 
		frame: true, 
		applyTo: 'myTroubleTicket_div',
		store: myTroubleTicketStore,
		cm: colModel, //列模型
		tbar: new Ext.Toolbar([
			{text: '添加', icon: '../images/add.gif', handler: showAdd},
			{text: '修改', icon: '../images/edit.gif', handler: showUpdate},
			{text: '删除', icon: '../images/delete.gif', handler: doDel},
			{text: '申请', icon: '../images/dispatch.gif', handler: doDispatch},
			{text: '审批历史', icon: '../images/viewHistory.gif', handler: viewHistory}
		]),
		bbar: new Ext.PagingToolbar({
			store: myTroubleTicketStore, 
			pageSize: 10,
			displayInfo: true,
			plugins: new Ext.ux.ProgressBarPager()
		})
	})
	
 	var historyStore = new Ext.data.JsonStore({
			url: '../approve/history.action',
			fields: ['desc', 'approveTime', 'approver']
		})
	
	var historyColModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		{header: '描述', width: 100, dataIndex: 'desc', sortable: true},
		{header: '审批时间', width: 100, dataIndex: 'approveTime', sortable: true},
		{header: '审批人', width: 100, dataIndex: 'approver', sortable: true}
	])
	
	var historyGrid = new Ext.grid.GridPanel({
		height: 200,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		frame: true, 
		store: historyStore,
		cm: historyColModel
	})
		
	var myTroubleTicketForm = new Ext.form.FormPanel({
		frame: true, 
		buttonAlign: 'center',
		defaults: {width: 200},
		items: [
			{
				xtype: 'textfield',
				name: 'troubleTicket.desc',
				fieldLabel: '描述',
				allowBlank: false
			},
			{
				xtype: 'combo',
				hiddenName: 'troubleTicket.level',
				mode: 'local', 
				fieldLabel: '级别',
				store: new Ext.data.JsonStore({ 
					data: [{name:'A'}, {name:'B'}, {name:'C'}],
					fields: ['name']
				}),
				displayField: 'name',
				valueField: 'name',
				triggerAction: 'all',
				typeAhead: true,
				forceSelection: true, 
				emptyText: '请选择...',
				allowBlank: false
			},
			{
				xtype: 'combo',
				hiddenName: 'troubleTicket.location',
				mode: 'local', 
				fieldLabel: '班级',
				store: new Ext.data.JsonStore({ 
					data: [{name:'A班'}, {name:'B班'}, {name:'C班'}],
					fields: ['name']
				}),
				displayField: 'name',
				valueField: 'name',
				triggerAction: 'all',
				typeAhead: true,
				forceSelection: true, 
				emptyText: '请选择...',
				allowBlank: false
			},
			{
				xtype:'datefield',
				name: 'troubleTicket.happenTime',
				fieldLabel: '发生时间',
				allowBlank: false,
				format: 'Y-m-d H:i'
				
			}
		],
		buttons: [
			{text: '保存', icon: '../images/save.gif', handler: doSave},
			{text: '关闭', icon: '../images/close.gif', handler: doClose}
		]	
	});		
	
	var win = new Ext.Window({
		width: 400,
		height: 260,
		items: [myTroubleTicketForm],
		modal: true, //模态
		resizable: false, //禁制调整窗口大小
		closeAction: 'hide' //不销毁form对象，只是隐藏
	})
	
	var historyWin = new Ext.Window({
		width: 400,
		height: 260,
		items: [historyGrid],
		modal: true, //模态
		resizable: false, //禁制调整窗口大小
		closeAction: 'hide' //不销毁form对象，只是隐藏
	})
	
	function showAdd() {
		myTroubleTicketForm.getForm().reset();
		win.show();
	}
	
	function showUpdate() {
	
	}
	
	function doSave() {
		myTroubleTicketForm.getForm().submit({
			url: '../myTroubleTicket/add.action',
			method: 'POST',
			clientValidation: true,
			waitTitle: '提示',
			waitMsg: '正在保存数据，请稍后...',
			success: function() {
				myTroubleTicketStore.reload(); //重新加载数据
				Ext.Msg.alert('提示', '保存请假单成功！', function() {
					win.hide();					
				});
			},
			failure: function() {
				Ext.Msg.alert('提示', '保存请假单失败！');
			}
		})	
	}
	
	function doClose() {
	
	}
	
	function doDel() {
	
	}
	
	function doDispatch() {
		var records = myTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择请假单！');
		}else {
			Ext.Msg.confirm('提示', '确认申请？', function(data) {
				if (data == 'yes') {
					var troubleTicketIds = [];	
					for (var i=0; i<records.length; i++) {
						troubleTicketIds.push(records[i].get('id'))
					}
					Ext.Msg.show({
						title: '提示',
						msg: '正在申请，请稍后...'
					})
					Ext.Ajax.request({
						url: '../myTroubleTicket/dispatch.action',
						method: 'GET',
						params: {troubleTicketIds:troubleTicketIds},
						callback: function(options, success, response ) {
							if (success) {
								myTroubleTicketStore.reload(); //重新装载
								Ext.Msg.alert('提示', '申请成功！');
							}else {
								Ext.Msg.alert('提示', '申请失败！');
							}
						}
					})
				}
			})
		}
	}
	
	function viewHistory() {
		var records = myTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择请假单！');	
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能选择一个请假单！');
		}else {
			historyWin.setTitle("审批信息列表");
			historyWin.show();
			historyStore.load({
				params:{"approveInfo.troubleTicket.id":records[0].get('id')}
			});	
		}
	}
})